package com.longteng.lesson2.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.longteng.lesson2.dao.InterfaceTestMapper;
import com.longteng.lesson2.domain.InterfaceTest;
import com.longteng.lesson2.service.InterfaceTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.lang.management.MonitorInfo;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/interfaceTest")
public class InterfaceTestController {
    @Autowired
    InterfaceTestMapper interfaceTestMapper;
    @Autowired
    InterfaceTestService interfaceTestService;

    @RequestMapping("/addView")
    public String addView() {
        return "/addInterfaceTest";
    }

    @RequestMapping("/add")
    public String addInterfaceTest(InterfaceTest interfaceTest) {
        String success = "success";
        try {
            interfaceTestMapper.addInterfaceTest(interfaceTest);
            System.out.println(interfaceTest.getInterfaceId());
        } catch (Exception e) {
            success = "fail";
            e.printStackTrace();
        }
        return success;
    }

    @RequestMapping("/delete")
    public String delete(Integer id) {
        String success = "success";
        try {
            interfaceTestMapper.deleteInterfaceTestById(id);
        } catch (Exception e) {
            success = "fail";
            e.printStackTrace();
        }
        return success;
    }

    @RequestMapping("/update")
    public String update(InterfaceTest interfaceTest) {
        String success = "success";
        try {
            interfaceTestMapper.update(interfaceTest);
        } catch (Exception e) {
            success = "fail";
            e.printStackTrace();
        }
        return success;
    }

    @RequestMapping("/select")
    public @ResponseBody
    Object select(InterfaceTest interfaceTest) {
        Object i = interfaceTestMapper.select(interfaceTest);
        return i;
    }

    @RequestMapping("/select1")
    public @ResponseBody
    Object select(String name) {
        Object i = interfaceTestMapper.getInterfaceAndCase(name);
        return i;
    }

    @RequestMapping("/selectForPage")
    public @ResponseBody Object selectForPage(Integer pageNum,Integer pageSize){
       Page page = PageHelper.startPage(pageNum,pageSize,true);
       List<InterfaceTest> interfaceTestList =interfaceTestMapper.select(null);
       Long total = page.getTotal();
       Map map =new HashMap();
       map.put("total",total);
       map.put("rows",interfaceTestList);

       for(InterfaceTest interfaceTest:interfaceTestList){
           String s  =interfaceTest.getInterfaceAliasName();
           System.out.println(",");
       }
       return map;
    }

    @RequestMapping("/test")
    public Object test(Integer i) throws Exception{
        return interfaceTestService.test(i);
    }

}
